package com.toggery.POS.controller;

import com.toggery.POS.entity.Role;
import com.toggery.POS.entity.User;
import com.toggery.POS.service.RoleService;
import com.toggery.POS.service.UserService;
import com.toggery.POS.tool.PasswordUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by LJH on 2018/3/18
 */
@RestController
//默认返回json格式
@RequestMapping("/user")
@Api(tags = "用户数据")
public class UserController {
    @Autowired
    UserService service;
    @Autowired
    RoleService roleService;

    @GetMapping("/list")
    public Map findAllUser(){
        Map map = new HashMap() ;
        map.put("data",service.findAllWithinRole());
        return map;
    }

    @GetMapping("/findbyname/{name}")
    public Map findByName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("date",service.findByName(name));
        return map;
    }

    @GetMapping("/findlikename/{name}")
    public Map findLikeName(@PathVariable String name){
        Map map = new HashMap() ;
        map.put("date",service.findLikeName(name));
        return map;
    }

    @PostMapping("/delete/{id}")
    public Map deleteById(@PathVariable int id){
        Map map = new HashMap();
        if(service.deleteByid(id)){
            map.put("result",true);
            map.put("msg","删除成功");
        }else {
            map.put("result",false);
            map.put("msg","删除失败");
        }
        return map;
    }

    @PostMapping("/update")
    @ResponseBody
    public Map updateUser(Model model, @RequestBody(required=false) Map map1){
        User user;
        user = service.findById(Integer.parseInt(map1.get("id").toString()));
        user.setUsername(map1.get("username").toString());
        HashMap map = new HashMap();
        if(service.updateUser(user)){
            map.put("result",true);
            map.put("msg","更新成功");
        }else {
            map.put("result",false);
            map.put("msg","更新失败");
        }
        return map;
    }

    @PostMapping("/save")
    public Map saveUser(Model model, @RequestBody(required=false) Map map1){
        User user = new User();
        Role role = roleService.findById(Integer.parseInt(map1.get("role").toString()));
        HashMap map = new HashMap();
        if(service.findById(Integer.parseInt(map1.get("id").toString()))!=null){
            user.setId(map1.get("id").toString());
            user.setUsername(map1.get("username").toString());
            user.setRole(role.getId()+"");
            user.setPassword(PasswordUtil.getPassword(user.getId(),"123"));
            if(service.save(user)){
                map.put("result",true);
                map.put("result","保存成功");
            }else {
                map.put("result",false);
                map.put("result","保存失败");
            }
        }else {
            map.put("result",false);
            map.put("result","保存失败");
        }

        return map;
    }

}
